<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>

<c:set var="ctx" value="${pageContext.request.contextPath}" />
<title>档案资料批量下载</title>
<jsp:include page="../../import.jsp" flush="true" />
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
</head>
<body class="iframeBody">
<div class="Wrapper">
	<div class="searchArea">
		<!-- 搜索表单开始 -->
		<form id="searchBar" enctype = "multipart/form-data">
			<div class="fieldset1 clearFix">
				<div class="FXBR">
					<div class="fieldBox">
						<div class="label">承租人名称</div>
						<div class="field">
							<input type="text" id="cstNm" name="cstNm" class="ui-textbox" ligerui="width: 160" value="">
						</div>
					</div>
					<div class="fieldBox">
						<div class="label">合同编号</div>
						<div class="field">
							<input type="text" id="contractNo" name="contractNo" class="ui-textbox" ligerui="width: 160" value="">
						</div>
					</div>
					<div class="fieldBox">
						<div class="label">支付表号</div>
						<div class="field">
							<input type="text" id="paySchNo" name="paySchNo" class="ui-textbox" ligerui="width: 160" value="">
						</div>
					</div>

					<div class="fieldBox">
						<div class="label">资产包</div>
		                    <div class="field">
                                <input id="upfile" name="upfile" style="width:150px" type="file">
								<span class="sq-button sq-button-hasicon"  onclick="ReadExcel();"><i class="sqBtnIco_9"></i>读取</span>  
								<span>
									<a id="upmodel" style="color:blue;text-decoration:underline;" href="<%=path %>/resource/template/reffile/paycddemo.xls" />模板样例下载</a>&nbsp;&nbsp;&nbsp;&nbsp; 
		                        </span>
		                        <input name="paySchCds" id="paySchCds" type="text" /> <!--value="${paySchCds}"-->
		                    </div>
						</div>
					</div>
					<div class="fieldBox">
						<div class="label">权证名称</div>
						<div class="field">
							<input name="searchCodeGrpCd" id="searchCodeGrpCd"  onclick="applyInvoice();"  type="text" ligerui="width: 160" >
<!--                                validate="{required:true}" -->
                        	<div class="combo_itemList_wrap"></div>
						</div>
					</div>
					<div class="fieldBox">
						<div class="label">事业部</div>
						<div class="field">
							<select id="buOrgCd"  name="buOrgCd"  ligerui="width: 160">
								<option value="">请选择</option>
								<c:forEach items="${buOrgCdList}" var="item">
									<option value="${item.key}">${item.value}</option>
								</c:forEach>
							</select>
						</div>
					</div>
<!-- 						<div class="fieldBox"> -->
<!-- 							<div class="label">省公司</div> -->
<!-- 							<div class="field"> -->
<!-- 								<select id="cstMgrOrgCd"  name="cstMgrOrgCd"  ligerui="width: 140"> -->
<!-- 									<option value="">请选择</option> -->
<%-- 									<c:forEach items="${prList}" var="item"> --%>
<%-- 										<option value="${item.key}">${item.value}</option> --%>
<%-- 									</c:forEach> --%>
<!-- 								</select> -->
<!-- 							</div> -->
<!-- 						</div> -->
					 <div class="fieldBox col_33" id="cstMgrOrgCdId" >
		                <div class="label">省公司</div>
		                <div class="field">
		                    <input name="cstMgrOrgCd" id="cstMgrOrgCd" type="text" class="ui-textbox" value="">
		                </div>
		            </div>
				</div>
				<div class="clearFix"></div>
				<!-- 表单按钮 -->
				<div class="btnsBox formBtns notRight">
					<span id="searchBtn" class="sq-button-round">查询</span>
					<span id="clearBtn" class="sq-button-round" onclick="clearBtnEvent();">清空</span>
					<input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
					<span id="downBtn" class="sq-button-round" onclick="downloadBtnEvent();">批量下载</span>
	            </div>
			</div>
		</form>
		<!-- 搜索表单结束 -->
	</div>
	<!-- 列表显示块 -->
	<div id="ligergrid1" class="sq-grid"></div>
</div>
<div  style="display:none">
	<div id= "configDilog" >
	   <form id="mainGoodsCd_formSet">
		<div class="fieldset1 tabel_col clearFix">
			<div class="fieldBox col_100_auto">
				<div class="label">主货物大类</div>
				<div class="field">
					<select name="mainGoodsCd" id="mainGoodsCd" data-value=" " onchange="getGoods();">
						<option value=" " checked >请选择</option>
						<c:forEach items="${mainGdsTypCdList }" var="kv">
							<option value="${kv.key }">${kv.value }</option>
						</c:forEach>
					</select>
				</div>
			</div>
			
			<div class="fieldBox col_100_auto">
				<div class="label">主运输货物</div>
				<div class="field">
					<select name="goods" id="goods" data-value=" " onchange="checkGoods();">
					</select>
				</div>
			</div>
			
		</div>
	 </form>
	</div>
</div>
<jsp:include page="../../system/sysUlFilesRec/sysUlFileRecList2.jsp" flush="true" >
     <jsp:param name="fflag" value="0"/>
     <jsp:param name="tabNm" value="ARC_MGR_DETAIL"/>
</jsp:include>
<script type="text/javascript">
	var pageInApp={
		flag:false //tab页验证关闭
	};//to下一级子窗口 接口
	var pageInTab=null;//页签
	var loadedTabs=[];//页签
	var grid = null;
	$(function () {
		initOrgTree();
		$("#paySchCds").css('display','none');
	    initForm();//初始化表单
	    initGrid();//注意初始化要在initForm之后,因为在初始化grid的时候需要form参数
 	    searchBtnEvent();//查询按钮事件
	    clearBtnEvent();//清空按钮
	});
	
	//初始化表单
	function initForm(){
		$("#searchBar").ligerForm();
	}
	
	//查询参数
	function getSearchParams(){
		var form = new liger.get("searchBar");
		var data = form.getData();
		return data;
	}
	
	//定义选择行数据-存支付表号
	var paySchNos = new Array();
	//加载表单数据
	function initGrid(){
		$("#ligergrid1").ligerGrid({
			url:'<%=path%>/warrant/warrantfilemanage/initListDownload.do',
			method:'post',
			parms:getSearchParams(),
			columns:[
				{ display: '承租人姓名',name: 'cstNm', minWidth: 30 , align:'center', enabledSort:true},
				{ display: '合同编号', name: 'contractNo', minWidth: 180 , align:'center', enabledSort:true},
				{ display: '支付表号', name: 'paySchNo', minWidth: 180 , align:'center', enabledSort:true},
				{ display: '放款时间', name: 'actPayTm', width: 100, align:'center', enabledSort:true},
				{ display: '事业部', name: 'buOrgCdNm', width: 120 , enabledSort:true},
				{ display: '省公司', name: 'prOrgCdNm', width: 120 , enabledSort:true},
				{ display: '电子版归档状态', name: 'eleStorageStatusNm', width: 80, align:'center', enabledSort:true},
				{ display: '纸质归档状态', name: 'storageStatusNm', width: 100, align:'center', enabledSort:true},
				{ display:'操作',isSort:false,width:100,align:'center',
					render:function(rowdata, rowindex, value, column) {
	            		return '<a onclick="toGetPath('+"'"+rowdata.contractNo+"'"+','+"'"+rowdata.paySchNo+"'"+')" >下载</a>';
	            	}	
				}
			],
			pageSize:10,
			root:'rows',
			record:'total',
			hideLoadButton:false,
			width:'100%',
			height:'100%',
			heightDiff:-2,
			checkbox:true,//列表是否有选择框
			rownumbers:true,//列表是否有行号
			frozen: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            onBeforeShowData: function () {
                var grid = this;
                grid.collapsedRows = [];
            },
            //选择行事件-JL-20181207
            onSelectRow : function (rowdata, rowid, rowobj){
            	var paySchNo = "'"+rowdata.paySchNo+"'";
            	//重复不添加
            	if(paySchNos.join().indexOf(paySchNo) == -1){
            		paySchNos[paySchNos.length] = paySchNo;
            		//$.ligerDialog.alert("paySchNos.length=["+paySchNos.length+"];paySchNos=["+paySchNos.join()+"]");//注释测试弹框
            	}
            },
            //取消选择行事件-JL-20181207
            onUnSelectRow : function (rowdata, rowid, rowobj){
				var paySchNo = "'"+rowdata.paySchNo+"'";
				//删除已存数据
    			for(var i=0;i<paySchNos.length;i++){
    				if(paySchNos[i] == paySchNo){
    					paySchNos.splice(i, 1);
    				}
    			}
    			//$.ligerDialog.alert("paySchNos.length=["+paySchNos.length+"];paySchNos=["+paySchNos.join()+"]");//注释测试弹框
            },
            //显示完数据事件-JL-20181207
            onAfterShowData: f_onAfterShowData,
            detail: {
                height: 'auto',
                onShowDetail: showDetail_grid
            }
		});
		grid = liger.get("ligergrid1");
		grid.setHeight(100);
	}
	
	//加载默认选中行-JL-20181207
    function f_onAfterShowData(currentData){
    	if(currentData!=null){
	    	var arr = eval(currentData);
	    	//$.ligerDialog.alert("currentData="+JSON.stringify(arr));//注释
	        var rows = arr.rows;
            var manager = $("#ligergrid1").ligerGetGridManager();
	        for(var i=0;i<rows.length;i++){
	        	var paySchNo = "'"+rows[i].paySchNo+"'";
	        	for(var j=0;j<paySchNos.length;j++){
	        		//对比支付表号
	        		if(paySchNos[j] == paySchNo){
	            		manager.select(i);//此时会调用onSelectRow选中行事件
	            		break;//节省时间
		        	}
	        	}
	        }
    	}
    }
	
	//加号展开列表
	 function showDetail_grid(row, detailPanel, callback) {
		 var fileName=$("#searchCodeGrpCd").val();
	     var gridBox = document.createElement('div');
	     $(gridBox).css('margin', 10);
	     $(detailPanel).css({
	         'padding': 10,
	         'box-sizing': 'border-box'
	     }).append(gridBox);
	     detailGrid = $(gridBox).ligerGrid({
	            url: '<%=path%>/warrant/warrantfilemanage/initArchiveList.do?contractNo=' + row.contractNo+'&paySchNo='+row.paySchNo+'&fileName='+fileName,
	         method: 'get',
	         columns: [
	             {display: '权证名称', name: 'fileName', width: 130, align: 'left'},
	             {display: '权证类别', name: 'fileFlagNm', width: 90, align: 'left'
	             },
	             {display: '资料类型', name: 'dossierTempNm', width: 90, align: 'left'},
	             {display: '要求归档份数', name: 'dossierCount', width: 90, align: 'left'},
	             {display: '归档中份数', name: 'dossierCountI', width: 90, align: 'left'},
	             {display: '存档份数', name: 'dossierCountT', width: 100, align: 'left'},
	             {display: '扫描件', name: 'arcLevelNm', width: 100, align: 'left'
		             ,
	            	 render:function(rowdata, rowindex, value, column) {
// 	            		 电子合同
	             		if(rowdata.filePath != null && rowdata.filePath !='' && rowdata.eleId == null){
	             			return '<a target="_downPdf'+rowdata.eleId+'" href="'+ rowdata.filePath+'" >查看PDF</a>';
	             		}
	             		//承租人(权证类别2)-身份证-客户id-权证资料-商业险	-项目id-权证资料-设备销售发票-prj_prd_dtl	
	             		else if(rowdata.ulFlag != null && rowdata.ulFlag =='1' && rowdata.ulFiles !=null && rowdata.ulFiles !=''){
	             			return ulFiles(rowdata.ulFiles);
	             		}
	             		else 
	             		if(rowdata.eleId != null && rowdata.eleId != '' && rowdata.filePath != null && rowdata.filePath !=''){
// 	            		 	return  '<a class="upfiles" id=""  onclick="setfiles('+"'"+rowdata.eleId+"'"+',this,'+"'"+rowdata.eleId+"'"+')"  >下载</a>';
	             			return '<a target="_downPdf'+rowdata.eleId+'" href="'+ rowdata.filePath+'" >查看</a>'; 
	             		}
	             	}	
		         }
	         ],
	         root: 'rows',
	         hideLoadButton: true,
	         width: '100%',
	         height: 'auto',
	         isScroll: false,
	         rownumbers: true,  //显示序号
	         checkbox: false,
	         allowUnSelectRow: true,
	         alternatingRow: true,
	         onAfterShowData: callback,
	         frozen: false,
	         usePager: false
	     });
	 }
	
	//遍历
	function ulFiles(list){
		if(list!=null&&list.length>0){
			var h="";
			for(var i=0;i<list.length && list[i].filePath!=null;i++){
				h+='<a target="_blank" href="'+list[i].filePath+'">查看</a>';
                h+='  ';
			}
		}
		return h;
	}

	
	//清空按钮事件
	function clearBtnEvent(){
		$("#clearBtn").click(function(event) {
			$("#resetBtn").click();  //重置 input-text
			var form = liger.get("searchBar");
			form.setData({  //重置select等特殊表单
				"buOrgCd":"",//事业部
				"cstMgrOrgCd":""//省公司
			});
		});
	}
	//查询按钮事件
	function searchBtnEvent(){
		$("#searchBtn").click(function(event) {
			var form = ""; 
			form= new liger.get("searchBar");
			var data = form.getData();
// 			alert(JSON.stringify(data));
			for (name in data){
				grid.setParm(name,data[name]);
			};
			grid.reload();
		});
	}
	
	
	
	
	//获取资产表中支付标号
	function ReadExcel(){ 
		$("#paySchCds").val("");//清空支付表号组
		$.ajaxFileUpload({  
		    url : "<%=path%>/warrant/warrantfilemanage/importExcel.do",  
		    dataType : 'json',  
		    data : {},
		    secureuri : false,  
		    fileElementId :'upfile',  
		    success : function(msg) { //服务器成功响应处理函数  
		    	 if(msg.success == 1){
		    		 if(msg.data == ""){
		    			 top.$.ligerDialog.SQalert({content: "上传的EXCEL支付表编号为空",SQ_type: "success"});
		    		 }else{
						 //赋值
			    		 $("#paySchCds").val(msg.data);
			    		 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
// 			    		 grid.reload();
		    		 }
    			 }else{
    				 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
    			 }  
	        }  
	    });  
	}
	
	//权证名称弹框
	function applyInvoice(){
		//弹出一个窗口
		$.ligerDialog.SQopen({
            title : '权证名称',
            width: 800,
            height:500,
            url:'<%=path%>/warrant/warrantfilemanage/toCompListPage.do?multiRow='+true,
            // 自定义buttons的时候 不要设置 fixButtons: true
            buttons: [
                {
                    text: "确定", cls: 'l-dialog-btn-ok', onclick:function(item, Dialog){
                    	//点击确定获取到选中的权证名称,如果没有获取到则提示
                    	if (Dialog.onOk) {
//                     		var cstMap = Dialog.onOk();
//                     		var kv = cstMap.keySet();
//                     		var cstNms="";
//                     		for(var i=0;i<kv.length;i++){
// 	                    		var cstNm = cstMap.get(kv[i]);
// 	                    		if(i == kv.length-1){//最后一次循环不加逗号
// 	                    			cstNms=cstNms+"'"+cstNm+"'";
// 	                    		}else{
// 	                    			cstNms=cstNms+"'"+cstNm+"',";
// 	                    		}
//                     		}
							//zrc2018-12-28翻页可记录前边选择
                    		var warrantNames = Dialog.onOk();
                    		var cstNms="";
                    		for(var i=0;i<warrantNames.length;i++){
	                    		var cstNm = warrantNames[i];
	                    		if(i == warrantNames.length-1){//最后一次循环不加逗号
	                    			cstNms=cstNms+cstNm;
	                    		}else{
	                    			cstNms=cstNms+cstNm+",";
	                    		}
                    		}
                    		if(StringUtils.isNotEmpty(warrantNames[0])){
                    			//关闭窗口
                    			Dialog.close();
                    			//页面赋值
//                     			alert("cstNms="+cstNms);
                    			$("#searchCodeGrpCd").val(cstNms);
                    		}else{
                    			UIUtils.alertWarning("未选择到信息,不能操作!");
                    		}
                   		
                    	}
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(item, Dialog){
                    	if (Dialog.onCancel) {
                    		Dialog.onCancel();
                    	}
                    }
                }
            ]
        });
	}
	//下载   
	 function toGetPath(contractNo,paySchNo){
		var fileName = $("#searchCodeGrpCd").val();
    	$.ajax({
    		type: 'post',
    		dataType: 'json',
            url: '<%=path%>/warrant/warrantfilemanage/downLoadOne.do?contractNo='+contractNo+'&paySchNo='+paySchNo+'&fileName='+fileName,		
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({
                    content: LogUtils.LOG_CMM_DATA_LOADING,
                    SQ_type: "waitting"
                });
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                 	window.open(msg.info);
                } else {
                	$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
	 }
// 	批量下载	(优化-JL-20181207)
		function downloadBtnEvent(){
			var fileName = $("#searchCodeGrpCd").val();
			var form = new liger.get("searchBar");
			var grid = liger.get("ligergrid1");
// 			var selectedRows = grid.getSelectedRows();
// 			var contractNos = new Array();
// 			var paySchNos = new Array();
// 			for(var i=0;i<selectedRows.length;i++){
// 				var contractNo = "'"+selectedRows[i].contractNo+"'";
// 				var paySchNo = "'"+selectedRows[i].paySchNo+"'";
// 				contractNos[i] = contractNo;
// 				paySchNos[i] = paySchNo;
// 			}
	        var data = form.getData();
// 	        data["contractNos"] = JSON.stringify(contractNos);
	        data["paySchNos"] = JSON.stringify(paySchNos);
	        data["fileName"] = fileName;
			if("[]" == JSON.stringify(paySchNos)){
				alert("未选择下载记录，请选择!");
			}else{
// 				 window.location.href 
// 				 <a href="ProSupPliers!downloadtfFileup.action?ID=$!file.FIL_ID">下载</a>
				 
				 
<%-- 				 window.location.href='<%=path%>/warrant/warrantfilemanage/downLoad.do'; --%>
// 				var wait_dialog = null;
				$.ajax({
					type: 'post',
					dataType:'json',
					url: '<%=path%>/warrant/warrantfilemanage/downLoad.do',
					data:data,
					beforeSend:function(){
						wait_dialog=$.ligerDialog.SQalert({content: '正在下载信息...',SQ_type: "waitting"});
					},
					success:function(msg){
						wait_dialog.close();
						if(msg.success == 1){
							window.open(msg.info);
						}else{
							$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
						}
					} 
				});
			}
			
		}
		function initOrgTree(){
	    	var params = {};
	    	 searchOrgTree = $("#cstMgrOrgCd").ligerComboBox({
	    		 width: 480,
	             selectBoxWidth: 300,
	             selectBoxHeight: 150,
	             //限制只能选叶节点，父节点不能点击
	             treeLeafOnly: false,
	             valueField: 'orgCd',
	             textField: 'orgDispNm',
	             showTreePath: true,
	             tree: {
	                 ajaxType: 'get',
	                 idFieldName:'orgCd',
	                 textFieldName: 'orgDispNm',
	                 checkbox: false,
	                 parentIcon: "none",
	                 childIcon: "none",
	                 needCancel : false
	             }
	         });
	    	 $.ajax({
	     		type: 'get',
	     		dataType:'json',
	     		data: params,
	     		url: '<%=path%>/warrant/warrantfilemanage/searchBuOrgTree.do',
	     		success:function(msg){
	     			if(msg.success == 1){
	     				searchOrgTree.treeManager.setData(msg.data);
	     				searchOrgTree.clear();
	     				searchOrgTree.setValue('${currOrg}');
	     			}else{
	     				 $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
	     			}
	     		}
	     	});

	    }    
		    
// 		//下载扫描件
			function setfiles(id,obj,pkId){//,fflag){
				var data={pkId:pkId};
// 					alert(JSON.stringify(data));
				var o = $(obj).parent();
				 $.ajax({
						type: 'get',
						dataType:'json',
						data:data,
						url: '<%=path%>/system/sysulfilesrec/initList.do',
						success:function(list){
							if(list!=null&&list!=''){
								var h="";
								for(var i=0;i<list.rows.length;i++){
									
									h+='<a href="javascript:uploadFileByUrl(';
					                h+="'"+list.rows[i].filePath+"'";
					                h+=",'"+list.rows[i].fileNm+"'";
					                h+=')">'+list.rows[i].fileNm+'</a>';
					                
// 						            //添加删除
// 					                if(fflag==1){
// 					                	h+=' | <a  href="javascript:uploadFileDeleteRow(';
// 						                h+="'"+list.rows[i].id+"'";
// 						                h+=",'"+id+"'";
// 						                h+=",'"+pkId+"'";
						                
// 						                h+=')"><i class="sqBtnIco_7"></i>删除</a>'  
// 						            }
					                h+='<br>';
								}
								$(o).html(h);
							}
						}
					});
			}
			function toGetPathConNo(contractNo){
		    	$.ajax({
		            type: 'post',
		            dataType: 'json',
		            url: '<%=path%>/esign/filSign/getPath.do?contractNo='+contractNo,
		            beforeSend: function () {
		                wait_dialog = $.ligerDialog.SQalert({
		                    content: LogUtils.LOG_CMM_DATA_LOADING,
		                    SQ_type: "waitting"
		                });
		            },
		            success: function (msg) {
		                wait_dialog.close();
		                if (msg.success == 1) {
		                	grid.reload($('.pcontrol input', grid.toolbar).val());//加载到当前页
		                	$.ligerDialog.SQalert({content: "下载成功", SQ_type: "success"});
		                    //window.open(msg.info);
		                	//window.open (msg.info,'newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
		                } else {
		                	$.ligerDialog.SQalert({content: "网络繁忙，请稍后再试", SQ_type: "error"});
		                }
		            }
		        });
		    }
</script>
</body>
</html>